/*-
 * Copyright (c) 2007 Martin Akesson <makesson@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#ifndef __NRG_CHUNKS_H
#define __NRG_CHUNKS_H

#include "cdtext.h"

typedef char chunkname_t[4];

typedef struct 
{
    chunkname_t id;
    uint32_t size;
} nrg_chunkheader_t;

// NERO chunk
typedef struct 
{
    chunkname_t id;
    uint32_t offset;
} nrg_nero_t;

// NER5 chunk
typedef struct 
{
    chunkname_t id;
    uint64_t offset;
} nrg_ner5_t;

// CUEX track data
typedef struct
{
    uint8_t  ctrladr;
    uint8_t  track; // BCD encoded; 00 = Lead in, AA = Lead out
    uint8_t  index;
    uint8_t  recdep;
    int32_t  start; // Negative for lead in
} __attribute__((__packed__)) nrg_cuex_trk_t;

// CUEX chunk "CUE sheet v2"
typedef struct
{
    nrg_chunkheader_t hdr;
    nrg_cuex_trk_t trackdata[];
} __attribute__((__packed__)) nrg_cuex_t;

// DAOI track data
typedef struct
{
    char     isrc[12]; // International Standard Recording Code
    uint16_t sector_size;
    uint8_t  track_mode;
    uint8_t  track_config;
    uint8_t  first_index;
    uint8_t  last_index;
    uint32_t index0;
    uint32_t index1;
    uint32_t next_track;
} __attribute__((__packed__)) nrg_daoi_trk_t;

// DAOI chunk "Disk at once information v1"
typedef struct
{
    nrg_chunkheader_t hdr;
    uint32_t be_size;
    char     mcn[12]; // Media Catalog Number
    uint8_t  toc_type;
    uint8_t  unknown1;
    uint8_t  unknown2;
    uint8_t  unknown3;
    uint8_t  first_track;
    uint8_t  last_track;
    nrg_daoi_trk_t trackdata[];
} __attribute__((__packed__)) nrg_daoi_t;

// DAOX track data
typedef struct
{
    char     isrc[12]; // International Standard Recording Code
    uint16_t sector_size;
    uint8_t  track_mode;
    uint8_t  track_config;
    uint8_t  first_index;
    uint8_t  last_index;
    uint64_t index0;
    uint64_t index1;
    uint64_t next_track;
} __attribute__((__packed__)) nrg_daox_trk_t;

// 2048 = MODE1 Data 
// 2352 = MODE1 Data Raw
// 2336 = MODE2 Data 2336

// DAOX chunk "Disk at once information v2"
typedef struct
{
    nrg_chunkheader_t hdr;
    uint32_t be_size;
    char     mcn[12]; // Media Catalog Number
    uint8_t  toc_type; // 0x00 = CD ROM?, 0x20 = SVCD?, 0x40 = DVD-ROM ?
    uint8_t  unknown1; // Session closed?
    uint8_t  unknown2; // Disc fixated?
    uint8_t  unknown3; //
    uint8_t  first_track;
    uint8_t  last_track;
    nrg_daox_trk_t trackdata[];
} __attribute__((__packed__)) nrg_daox_t;

// CDTX chunk "CD Text"
typedef struct
{
    nrg_chunkheader_t hdr;
    cdtext_pack_t cdtext[];
} __attribute__((__packed__)) nrg_cdtx_t;

// ETNF chunk "Extended track information v1"
typedef struct
{
    nrg_chunkheader_t hdr;
    uint32_t offset;
    uint32_t length;
    uint32_t mode;
    uint32_t start_lsn;
    uint32_t unknown1;
} nrg_etnf_t;

// ETN2 chunk "Extended track information v2"
typedef struct
{
    nrg_chunkheader_t hdr;
    uint64_t offset;
    uint64_t length;
    uint32_t mode;
    uint32_t start_lsn;
    uint32_t unknown1;
} nrg_etn2_t;

// MTYP chunk "Medium type"
typedef struct
{
    nrg_chunkheader_t hdr;
    uint32_t type; // 0x1 = CD (old format), 0x6 = DVD-R/RW, 0x1c = DVD (Old format)
} nrg_mtyp_t;

// SINF chunk "Session information"
typedef struct
{
    nrg_chunkheader_t hdr;
    uint32_t tracks; 
} nrg_sinf_t;

#endif
